package com.jmb.apiplatform.util;


import com.alibaba.fastjson.JSONObject;
import com.jmb.apiplatform.exception.ValidException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ValidUtil {

    // 判断数据是否为空，如果为空抛出异常，异常信息是我们自定义
    public static void notNUll(Object object, String msg) throws ValidException {
        if (object == null ) {
            throw new ValidException(msg);
        }
    }
    public static void length(String s, int minLength, int maxLength, String msg) throws ValidException {
        notNUll(s, msg);
        String trim = s.trim();
        int sLen = trim.length();
        if (sLen > maxLength || sLen < minLength) {
            throw new ValidException(msg);
        }
    }


    public static void notEmpty(String s, String msg) throws ValidException {
        notNUll(s, "传入的数据不能为空");
        if (s.isEmpty()){
            throw new ValidException(msg);
        }
    }
    public static void size(Byte num, Integer min, Integer max, String msg) throws ValidException {
        if (num < min || num > max) {
            throw new ValidException(msg);
        }
    }
    public static void size(Byte num, Integer max, String msg) throws ValidException {
        size(num, 0, max, msg);
    }
    public static void size(Integer num, Integer min, Integer max, String msg) throws ValidException {
        if (num < min || num > max) {
            throw new ValidException(msg);
        }
    }
    public static void size(Integer num, Integer max, String msg) throws ValidException {
        size(num, 0, max, msg);
    }
    public static void size(Long num, Integer min, Integer max, String msg) throws ValidException {
        if (num < min || num > max) {
            throw new ValidException(msg);
        }
    }
    public static void size(Long num, Integer max, String msg) throws ValidException {
        size(num, 0, max, msg);
    }
    public static void size(Short num, Integer min, Integer max, String msg) throws ValidException {
        if (num < min || num > max) {
            throw new ValidException(msg);
        }
    }
    public static void size(Short num, Integer max, String msg) throws ValidException {
        size(num, 0, max, msg);
    }
    public static void isJsonPath(String jsonPath) throws ValidException {
        notNUll(jsonPath, "json path 不能为空");
        notEmpty(jsonPath, "json path 不能为空");
        if (!jsonPath.startsWith("$")){
            throw new ValidException("json path" +  jsonPath+ "必须以$开头");
        }
    }
    public static void isXmlPath(String xmlPath) throws ValidException {
        notNUll(xmlPath, "xml path 不能为空");
        notEmpty(xmlPath, "xml path 不能为空");
        if (!xmlPath.startsWith("//")){
            throw new ValidException("xml path" +  xmlPath+ "必须以//开头");
        }
    }
    public static void isJsonObject(String jsonString, String errorMsg) throws ValidException {
        try {
            JSONObject.parseObject(jsonString);
        } catch (Exception e) {
            if (errorMsg == null) {
                throw new ValidException("json 数据 错误信息! " + e.getMessage());
            } else {
                throw new ValidException(errorMsg);
            }
        }
    }

    public static void isJsonObject(String jsonString) throws ValidException {
        isJsonObject(jsonString, null);
    }

    public static void isJsonArray(String jsonString, String errorMsg) throws ValidException {
        try {
            JSONObject.parseArray(jsonString);
        } catch (Exception e) {
            if (errorMsg == null) {
                throw new ValidException("json 数据 错误信息 " + e.getMessage());
            } else {
                throw new ValidException(errorMsg);
            }
        }
    }

    public static void isJsonArray(String jsonString) throws ValidException {
        isJsonArray(jsonString, null);
    }

    public static void isJson(String jsonString, String errorMsg) throws ValidException {
        boolean isObject = true;
        boolean isArray = true;
        String msg = "";
        try {
            isJsonObject(jsonString);
        } catch (ValidException e) {
            isObject = false;
            msg += e.getMessage();
        }
        try {
            isJsonArray(jsonString);
        } catch (ValidException e) {
            isArray = false;
            msg += e.getMessage();
        }
        if (!isObject && !isArray) {
            if (errorMsg == null) {
                throw new ValidException(msg);
            } else {
                throw new ValidException(errorMsg);
            }
        }
    }

    public static void isJson(String jsonString) throws ValidException {
        isJson(jsonString, null);
    }

}
